package com.itheima.question.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.QuestionDto;
import com.itheima.admin.dto.QuestionItemDto;
import com.itheima.admin.vo.QuestionVo;
import com.itheima.question.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @ClassName QuestionController
 * @Description TODO
 * @Author Mister-Lu
 * @Date 2021/12/21
 **/
@RestController
@RequestMapping("/question")
@Api(value = "管理后台题目模块接口", tags = "question", description = "用于实现后台题目的增删改查操作")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/list")
    @ApiOperation("分页显示题目列表")
    public PageVo<QuestionVo> queryByPage(@RequestBody PageDto pageDto){
        return questionService.queryByPage(pageDto);
    }

    @PutMapping("/edit")
    @ApiOperation("编辑题目")
    public Result editQuestion(@RequestBody QuestionDto questionDto){
        return questionService.editQuestionById(questionDto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除题目")
    public Result delById(@PathVariable("id") String id) {
        return questionService.delById(id);
    }

    @PostMapping("/add")
    @ApiOperation("添加题目")
    public Result addQuestion(@RequestBody QuestionDto questionDto) {
        Set id = redisTemplate.boundSetOps("id").members();
        System.out.println(id);
        return questionService.questionAdd(questionDto);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查找题目")
    public QuestionDto queryById(@PathVariable("id") String id){
        return questionService.queryById(id);
    }

    @PostMapping("/add2")
    @ApiOperation("添加题目item")
    public Result addQuestion2(@RequestBody QuestionItemDto questionItemDto) {
        Set id = redisTemplate.boundSetOps("questionId").members();
        System.out.println(id);
        return questionService.questionAdd2(questionItemDto);
    }


    @GetMapping("/{questionId}")
    @ApiOperation("根据id查找题目")
    public QuestionItemDto queryItemById(@PathVariable("questionId") String questionId){
        return questionService.queryItemById(questionId);
    }
}
